例子
use Doctrine\ORM\Mapping as ORM;/** * User (用户) * @ORM\Entity * @ORM\Table */class User{ /** * 一对一关联 Profile (资料),`mappedBy` 反向关联 Profile->$user,User 为主键 * @ORM\OneToOne(targetEntity="Profile", mappedBy="user") */ protected $profile; /** * 一对多关联 OAuth (第三方登录),`mappedBy` 反向关联 OAuth->$user,User 为主键 * @ORM\OneToMany(targetEntity="OAuth", mappedBy="user") */ protected $oauths; /** * 多对多关联 Group (用户组),JoinTable 创建关联表 * @ORM\ManyToMany(targetEntity="Group") * @ORM\JoinTable(name="user_group_relation", * joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")} */ protected $groups;}
use Doctrine\ORM\Mapping as ORM;/** * Profile (资料) * @ORM\Entity * @ORM\Table */class Profile{ /** * 一对一关联 User (用户),`inversedBy` 正向关联 User->$profile,Profile 为外键(`Profile.user_id` = `User.id`) * @ORM\OneToOne(targetEntity="User", inversedBy="profile") * @ORM\JoinColumn(name="user_id", referencedColumnName="id") */ protected $user;}
use Doctrine\ORM\Mapping as ORM;/** * OAuth (第三方登录) * @ORM\Entity * @ORM\Table */class OAuth{ /** * 多对一关联 User (用户),`inversedBy` 正向关联 User->$oauths,OAuth 为外键(`OAuth.user_id` = `User.id`) * @ORM\ManyToOne(targetEntity="User", inversedBy="oauths") * @ORM\JoinColumn(name="user_id", referencedColumnName="id") */ protected $user;}
use Doctrine\ORM\Mapping as ORM;/** * Group (用户组) * @ORM\Entity * @ORM\Table */class Group{ /** * 多对多关联 User,还未测试,待续 * @ORM\ManyToMany(targetEntity="User") */ protected $users;}